package org.example.statistics.service.impl;

import java.util.Calendar;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.example.contract.domain.Contract;
import org.example.contract.mapper.ContractMapper;
import org.example.utils.UserType;
import org.example.utils.Util;
import org.example.warehouse.domain.SellStoreDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.statistics.mapper.StatisticsSellMapper;
import org.example.statistics.domain.StatisticsSell;
import org.example.statistics.service.IStatisticsSellService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 统计销售报表Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-05-31
 */
@Service
public class StatisticsSellServiceImpl implements IStatisticsSellService
{
    @Autowired
    private StatisticsSellMapper statisticsSellMapper;


    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询统计销售报表
     *
     * @param statisticsSellId 统计销售报表主键
     * @return 统计销售报表
     */
    @Override
    public StatisticsSell selectStatisticsSellByStatisticsSellId(Long statisticsSellId)
    {
        return statisticsSellMapper.selectStatisticsSellByStatisticsSellId(statisticsSellId);
    }

    /**
     * 查询统计销售报表列表
     *
     * @param statisticsSell 统计销售报表
     * @return 统计销售报表
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<StatisticsSell> selectStatisticsSellList(StatisticsSell statisticsSell)
    {
        if("1".equals(statisticsSell.getRemark())){
            statisticsSell.getParams().put("role",1);
        }else{
            statisticsSell.getParams().put("role",0);
        }
       // return statisticsSellMapper.selectStatisticsSellList(statisticsSell);
        return statisticsSellMapper.queryStatisticsSellByGroupByMonth(statisticsSell);
    }

    /**
     * 新增统计销售报表
     *
     * @param statisticsSell 统计销售报表
     * @return 结果
     */
    @Override
    public int insertStatisticsSell(StatisticsSell statisticsSell)
    {
        statisticsSell.setCreateTime(DateUtils.getNowDate());
        return statisticsSellMapper.insertStatisticsSell(statisticsSell);
    }

    /**
     * 修改统计销售报表
     *
     * @param statisticsSell 统计销售报表
     * @return 结果
     */
    @Override
    public int updateStatisticsSell(StatisticsSell statisticsSell)
    {
        return statisticsSellMapper.updateStatisticsSell(statisticsSell);
    }

    /**
     * 批量删除统计销售报表
     *
     * @param statisticsSellIds 需要删除的统计销售报表主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsSellByStatisticsSellIds(String statisticsSellIds)
    {
        return statisticsSellMapper.deleteStatisticsSellByStatisticsSellIds(Convert.toStrArray(statisticsSellIds));
    }

    /**
     * 删除统计销售报表信息
     *
     * @param statisticsSellId 统计销售报表主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsSellByStatisticsSellId(Long statisticsSellId)
    {
        return statisticsSellMapper.deleteStatisticsSellByStatisticsSellId(statisticsSellId);
    }

    @Override
    @Transactional
    public int insertStatisticsSellByMonth(StatisticsSell statisticsSell) {
        int count=0;

        Contract contract= new Contract();
        contract.setCreateTime(statisticsSell.getCreateTime());
        contract.setContractType(Util.CONTRACT_TYPE_SELL);
        List<Contract> contracts = contractMapper.selectStatisticsContractList(contract);
        statisticsSellMapper.batchInsert(contracts);
        count++;


        //统计入库单
        SellStoreDetail sellStoreDetail_1 =new SellStoreDetail();
        sellStoreDetail_1.setCreateTime(statisticsSell.getCreateTime());
        List<StatisticsSell> list1 = statisticsSellMapper.selectStatisticsSellStoreTotalMoneyByMonth(sellStoreDetail_1);
        if(list1 !=null && list1.size()>0){
            statisticsSellMapper.batchUpdate(list1);
            count++;
        }

        //统计对账单
        List<StatisticsSell> list2 = statisticsSellMapper.selectStatisticsSellAccountTotalMoneyByMonth(sellStoreDetail_1);
        if(list2 !=null && list2.size()>0){
            statisticsSellMapper.batchUpdate(list2);
            count++;
        }


        //统计支付
        statisticsSell.setCreateTime(statisticsSell.getCreateTime());
        List<StatisticsSell> list3 = statisticsSellMapper.selectStatisticsPayMoneyByMonth(statisticsSell);
        if(list3 !=null && list3.size()>0){
            statisticsSellMapper.batchUpdate(list3);
            count++;
        }


        return count;
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<StatisticsSell> queryStatisticsSellByGroupByMonth(StatisticsSell statisticsSell) {
        if(Util.isGongYingShangRole()){
            statisticsSell.setRemark(UserType.GONGYINGSHANG.getValue());
            statisticsSell.getParams().put("deptId", ShiroUtils.getSysUser().getDeptId());
        }
        return statisticsSellMapper.queryStatisticsSellByGroupByMonth(statisticsSell);
    }
    @Override
    @DataScope(deptAlias = "d")
    public StatisticsSell selectStatisticsSellByCurrentMonth(StatisticsSell statisticsSell){
        if(Util.isGongYingShangRole()){
            statisticsSell.setRemark("1");
        }else{
            statisticsSell.setRemark("0");
        }
        statisticsSell.setCreateTime(DateUtils.getNowDate());
        return statisticsSellMapper.selectStatisticsSellByCurrentMonth(statisticsSell);
    }

}
